package com.example.myapplication.dialogs;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;

public class ForceDailog {

    private static ForceDailog forceDailog;
    public static ForceDailog init(){
        if(forceDailog==null){
            forceDailog = new ForceDailog();
        }
        return forceDailog;
    }

    private Context context;
    private Activity activity;
    private PopupWindow window;
    public void showDailog(final Context mcontext, Activity mactivity) {
        this.context = mcontext;
        this.activity = mactivity;
        // 利用layoutInflater获得View
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View popView = inflater.inflate(R.layout.force_dialog, null);
        window = new PopupWindow(popView,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT);
        // 设置popWindow弹出窗体可点击
        window.setFocusable(true);
        window.setOutsideTouchable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        window.setBackgroundDrawable(dw);
        //设置popWindow的显示和消失动画
        window.setAnimationStyle(R.style.mypopwindow_anim_style);
        //更新显示信息
        TextView tv_user_di_que = popView.findViewById(R.id.tv_user_di_que);
        tv_user_di_que.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeLightclose(activity);
                window.dismiss();
                //调转选车界面
                Toast.makeText(mcontext,"前往选车界面",Toast.LENGTH_SHORT).show();
            }
        });
        RelativeLayout rl_dialog_root = popView.findViewById(R.id.rl_dialog_root);
        rl_dialog_root.setOnClickListener(new View.OnClickListener() {//点击确认
            @Override
            public void onClick(View view) {
            }
        });
        // 在底部显示
        window.showAtLocation(mactivity.findViewById(R.id.root_layout),
                Gravity.BOTTOM, 0, 0);
        //恢复背景亮度
        changeLightShow(activity);

        //popWindow消失监听方法
        window.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                changeLightclose(activity);
                System.out.println("popWindow消失");
            }
        });
    }

    public void dissDailog(){
        if(activity!=null){
            changeLightclose(activity);
        }
        if(window!=null){
            window.dismiss();
        }
    }

    /**
     * 背景变暗,并增加渐变动画
     */
    public void changeLightShow(Activity activity) {
        final ValueAnimator animation = ValueAnimator.ofFloat(1.0f, 0.6f);
        animation.setDuration(200);
        animation.start();
        final Window window = activity.getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();
        animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                lp.alpha = (float) valueAnimator.getAnimatedValue();
                window.setAttributes(lp);
            }
        });
    }

    /**
     * 恢复背景,并增加渐变动画
     */
    public void changeLightclose(Activity activity) {
        final ValueAnimator animation = ValueAnimator.ofFloat(0.6f, 1.0f);
        animation.setDuration(200);
        animation.start();
        final Window window = activity.getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();
        animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                lp.alpha = (float) valueAnimator.getAnimatedValue();
                window.setAttributes(lp);
            }
        });
    }
}
